package cn.itsource.org.controller;

import cn.hutool.core.util.StrUtil;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController //@Controller + @ResponseBody的结合体
@RequestMapping("/dept") //暴露公共路径
@Api(tags = "部门管理接口")  //swagger API
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门无限极树") //swagger API
    public AjaxResult deptTree(){

        //查询
        List<Department> list = departmentService.deptTree();
        //返回
        return AjaxResult.createSuccess(list);
    }
    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有的部门") //swagger API
    public AjaxResult list(){

        //查询
        List<Department> list = departmentService.findAll();
        //返回
        return AjaxResult.createSuccess(list);
    }
    /**
     * 查询部门分页
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询部门分页") //swagger API
    public PageList<Department> findPage(@RequestBody DepartmentQuery query){
        System.out.println(query);
        //查询
        return departmentService.findPage(query);
    }


    /**
     * 新增/修改  ，根据id区分
     * @return
     * @Valid
     */
    @PostMapping
    @ApiOperation(value = "新增修改部门，新增没有id，修改有id") //swagger API
    public AjaxResult saveOrUpdate(@Valid @RequestBody Department department){

        /*
        方案一：可以但是没有必要
        if (StrUtil.isBlank(department.getName())){
            return AjaxResult.createError("请输入部门名称");
        }
        if (StrUtil.isBlank(department.getSn())){
            return AjaxResult.createError("请输入部门编号");
        }
*/
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }

    }
    /**
     * 删除
     * @return
     */
    @ApiOperation(value = "根据id删除部门对象") //swagger API
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            departmentService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }
    /**
     * 批量删除
     * 前端传递的是 {ids:"123,123"}  这个叫对象
     * 后台必须用对象接受
     * @return
     */
    @ApiOperation(value = "根据ids批量删除部门对象") //swagger API
    @PostMapping("/batchRemove")
    public AjaxResult delete(@RequestBody DepartmentQuery query){

        try {
            //批量删除
            departmentService.batchRemove(query);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }
}
